package hl.games.tankwar.core;



import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

public  class Util {
	static MediaPlayer mp;
	private static SoundPool soundPool;
	private static HashMap<Integer, Integer> soundPoolMap;
	
	
	public static void playBGM(Context context,int id, boolean looping) {

		if(mp == null) {

		mp=MediaPlayer.create(context, id);

		}

		mp.setLooping(looping);

		if(!mp.isPlaying()){
			mp.start();
		}
		else {
			mp.stop();
		}

		}

	public static void stopBGM() {
		if(mp != null) {
			mp.release();
			mp = null;
		}
	}
	
	public static void play(int id)
	{
		if(soundPool!=null&&soundPoolMap!=null)
		{
			soundPool.play(soundPoolMap.get(id), 1, 1, 0, 0, 1);
		}
		
	}
	
	public static void loadSoundPool(Context context,int[] ids) throws Exception{
		if(soundPool==null)
		{
			soundPool=new SoundPool(2, AudioManager.STREAM_MUSIC,100);
		}
		if (soundPoolMap==null) {
			soundPoolMap=new HashMap<Integer, Integer>();
		}
		for (int id : ids) {
			soundPoolMap.put(id, soundPool.load(context, id, 0));
		}
	}
}
